<?php

/*
	Class: User
*/
class User extends ActiveModel {
	protected $_tableName = 'users';
	protected $_className = 'User_Record';
}

/*
    Class: User_Validation
    Define validation rules
*/
class User_Validation extends ActiveRecordValidation {
    public static $insert_rules = array(
        'username'      => 'trim|required|min_length[2]|max_length[128]|callback_check_username',
        'account_id'    => 'trim|required|integer|account_exists',
        'password'      => 'trim|required|min_length[4]|max_length[128]|matches[passconf]|md5'
    );
    public static $update_rules = array(
        'username'      => 'trim|min_length[2]|max_length[128]|callback_check_username',
        'account_id'    => 'trim|integer|account_exists',
        'password'      => 'trim|min_length[4]|max_length[128]|matches[passconf]|md5'
    );
    public static $fields = array(
        'account_id'    => 'account id',
        'passconf'      => 'confirmation'
    );
}

/*
	Class: User_Record
	ActiveRecord instance
*/
class User_Record extends ActiveRecord {

	protected $_tableName = 'users';
	
	/*
        Function: preCreate
        PreCreate Event encrypts the password, and sets the created_at and updated_at
    */
    protected function preInsert()
    {
        $this->created_at = date('Y-m-d H:i:s');
        $this->updated_at = date('Y-m-d H:i:s');
    }
    
    /*
        Function: preUpdate
        PreUpdate Event encrypts the password if it is being updated
    */
    protected function preUpdate()
    {
        $this->updated_at = date('Y-m-d H:i:s');
    }
}